using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Hmw;
using net_work.IService.Hmw;

namespace net_work.Controllers.Hmw;

[Route("api/[controller]")]
[ApiController]
public class BankQuestionController(ILogger<BankQuestionController> logger, IBankQuestionService bankQuestionService)
    : ControllerBase
{
    /// <summary>
    /// 获取题库题目列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListBankQuestions([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取题库题目列表");
        var result = await bankQuestionService.GetBankQuestionsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取题库题目信息
    /// </summary>
    /// <param name="bankQuestionId">题库题目ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetBankQuestionById([FromQuery] long bankQuestionId)
    {
        logger.LogInformation("获取题库题目信息");
        return await bankQuestionService.SelectByIdAsync(bankQuestionId) is { } bankQuestion
            ? AjaxResult.Success(bankQuestion)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增题库题目
    /// </summary>
    /// <param name="bankQuestionAddRequest">新增题库题目实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> AddBankQuestion([FromBody] BankQuestionAddRequest bankQuestionAddRequest)
    {
        logger.LogInformation("新增题库题目");
        var bankQuestionId = await bankQuestionService.InsertBankQuestion(bankQuestionAddRequest);
        return bankQuestionId > 0
            ? AjaxResult.Success(new { bankQuestionId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改题库题目
    /// </summary>
    /// <param name="bankQuestionUpdateRequest">修改题库题目实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> UpdateBankQuestion([FromBody] BankQuestionUpdateRequest bankQuestionUpdateRequest)
    {
        logger.LogInformation("修改题库题目");
        return await bankQuestionService.UpdateBankQuestion(bankQuestionUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除题库题目
    /// </summary>
    /// <param name="ids">题库题目ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> DeleteBankQuestion([FromBody] List<long> ids)
    {
        logger.LogInformation("删除题库题目");
        return await bankQuestionService.DeleteBankQuestionByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}